Make percentage string values as floats/ints in InfluxDB#7879
Conversation
Currently Z-wave and other compontents report an attributes battery
level as an integer, for example
```yaml
{
"is_awake": false,
"battery_level": 61,
}
```
However, some other components like Vera add the battery level as a
string
```yaml
{
"Vera Device Id": 25,
"device_armed": "False",
"battery_level": "63%",
"device_tripped": "False",
}
```
By removing any % signs in the field, this will send the value to
InfluxDB as an int, which can then be used to plot the data in graphs
correctly, like other percentage fields.
|
@philhawthorne, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fabaff, @balloob and @titilambert to be potential reviewers. |
|
There's a test for InfluxDB, could you update that also, please? |
|
Is it the correct approach to do this parsing in Influxdb? Maybe each component or base component should make sure not to include units in the values reported? |
|
That only really works for Would be good to have something like a dict with a unit, however this would be quite a big change as attributes are just key with simple value at the moment. |
|
Yes, it wouldn't be a quick fix. If we add the fix here, can we make it broader somehow, to not only target
|
|
I thought about that. I was worried this would happen {
"friendly_name": "Living Lamp 1"
},
{
"friendly_name": "Moto 2X9P"
}Would just become {
"friendly_name": 1
},
{
"friendly_name": 29
}I guess we could keep storing the {
"friendly_name": 29,
"friendly_name_str": "Moto 2X9P"
}But not sure if adding so many extra keys would be a good idea? |
|
@philhawthorne I think we should only strip trailing characters, and also store the original string 👍 |
|
@lwis yep that makes sense! I'll make the changes and update the tests to check for it as well |
Adds tests and now removes all trailing non-numeric characters for better use
|
Added tests and now removes all trailing non-digits from the attribute. For example {
"battery_level": "44%",
"friendly_name": "Johns iPhone 2"
}Will become {
"battery_level_str": "44%",
"battery_level": 44,
"friendly_name_str": "Johns iPhone 2"
} |
| ] | ||
|
|
||
| percent_remove_regex = re.compile(r"^([0-9.])+\%$") | ||
| percent_remove_regex = re.compile(r'[\d.]+') |
There was a problem hiding this comment.
You should call this something else now, right? non_digit_tail?
There was a problem hiding this comment.
Correct! Cheers. Have updated that
Updates the variable used for the regex to remove trailing non digits
|
I'm happy with this now, it seems like a sensible change. @MartinHjelmare do you have anything else, or can this be merged? |
|
If the regex works I'm happy. There's a small lint error that needs fixing before we can merge. |
There was a problem hiding this comment.
think it is fine to cast the type for influxdb. But we should also fix the Vera component/platform. The state should not be include any types, that is a bug of device and need to be fix.
In this case, remove the *_str. That should be the same as the casted type. All other is a bug that need fix on right position and not inside influxdb. But with this new cast it will make that component more robust.
Fixes a small linting error on the InfluxDB component
|
@pvizeli should we update the Vera plugin in this Pull Request? This PR will cover all components that may have this problem. Wondering if it would be better to separate the Vera updates into a different PR as it may be a breaking change? |
|
Yes, the fix on other component/platform should be done in a new PR. I see that the |
|
this change produces many new error for entities that have IP-addresses as attribute, because the regex does not consider strings with more than one dot as not convertible to floats. it seems to just tries to convert everything with a dot in it to a float. |
Description:
Currently Z-wave and other components report an attributes battery
level as an integer, for example
However, some other components like Vera add the battery level as a
string
By removing any % signs in the field, this will send the value to
InfluxDB as an int, which can then be used to plot the data in graphs
correctly, like other percentage fields.
This PR will detect when a percentage is being cast as a string, and remove
the % sign before it is shipped over to InfluxDB.
Not really a Python dev, so if there's a better way to do this, or some tests I
need to add any help would be appreciated.
Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass